exports.master = function (req, res) {
//达人主页
    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;
    //最终结果
    var resultData = {};

    var masterData = 0;

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;
    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;
    //任务流创建
    async.waterfall([

        function (cb) {
            if (restfulParams.u_type == 1) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganization + "/o_uid/" + restfulParams.m_uid,
                        method: 'POST',
                        data:{
                            fields:["o_name as u_nickname", "o_logo as u_header_url", "o_desc as u_sign", "o_tags as m_professionals", "o_browse_times as m_browse_count", "o_concern_count as m_fans_count"].join(",")
                        }
                    },
                    function (err, data) {

                        masterData = data.data;

                        resultData.masterData = data.data;

                        resultData.isMaster = 1;
                        cb();

                    }
                );
            } else {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_master + "/m_uid/" + restfulParams.m_uid,
                        method: 'GET'
                    },
                    function (err, data) {

                        masterData = data.data;

                        if (masterData.length > 0) {

                            resultData.masterData = data.data;

                            resultData.isMaster = 1;
                            cb();
                        } else {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_user_info + "/u_id/" + restfulParams.m_uid,
                                    method: 'POST',
                                    data: {
                                        fields: ["u_nickname", "u_header_url", "u_sign"].join(",")
                                    }
                                },
                                function (err, data) {

                                    masterData = data.data;

                                    resultData.masterData = data.data;

                                    resultData.isMaster = 0;
                                    cb();
                                }
                            );
                        }
                    }
                );
            }

        },
        //获取达人回答问题的列表
        function (cb) {
            var isMaster = resultData.isMaster;
            if (isMaster == 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_content + "/fc_uid/" + restfulParams.m_uid + "/skip/" + skip + "/limit/" + limit,
                        method: 'POST',
                        data: {
                            fields: [
                                "fc_id",
                                "fc_uid",
                                "fc_title",
                                "fc_images",
                                "fc_type",
                                "fc_answer_count",
                                "fc_create_time",
                                "fc_content",
                                "u_header_url",
                                "u_nickname"
                            ].join(",")
                        }

                    },
                    function (err, data) {

                        resultData.contentListData = data.data;

                        cb();
                    }
                );
            } else {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_answer_userinfo + "/fca_uid/" + restfulParams.m_uid + "/skip/" + skip + "/limit/" + limit + "/sort_order/fca_create_time-desc",
                        method: 'POST',
                        data: {
                            fields: [
                                "fca_id",
                                "fca_uid",
                                "fca_content",
                                "fca_images",
                                "fcc_fcid",
                                "fca_create_time"
                            ].join(",")
                        }

                    },
                    function (err, data) {

                        resultData.answerListData = data.data;

                        cb();
                    }
                );

            }
        },

        function (cb) {

            var answerListData = resultData.answerListData;
            var contentListData = resultData.contentListData;
            if (!contentListData && answerListData.length > 0) {
                var tasks = [];

                answerListData.forEach(function (item, i) {

                    tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchForumContent + "/fc_id/" + item.fcc_fcid,
                                method: 'POST',
                                data: {
                                    fields: ["fc_id", "fc_title", "fc_answer_count"].join(",")
                                }
                            },
                            function (err, data) {

                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }

                                var questionData = data.data;

                                item.questionData = questionData;

                                cb();
                            }
                        );
                    })
                });
                async.waterfall(tasks, cb);
            } else {
                cb()
            }
        },
        //查询当前用户关注达人的状态
        function (cb) {
            if (masterData.length > 0) {
                if (restfulParams.u_id) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchMasterConcern + "/is_delete/0/mc_master_id/" + restfulParams.m_uid + "/mc_uid/" + restfulParams.u_id,
                            method: 'POST',
                            data: {
                                fields: ['mc_id'].join(",")
                            }

                        },
                        function (err, data) {

                            var is_zan = data.data;

                            if (is_zan.length <= 0) {
                                resultData.isConcern = 0;
                            } else {
                                resultData.isConcern = 1;
                            }
                            cb();
                        }
                    );
                } else {
                    cb();
                }
            } else {
                resultData.isConcern = null;
                cb();
            }
        },
    ], function (err, result) {

        //返回最终拼接结果
        RES.response(res, true, resultData);

    });

}